package org.jeecg.modules.solitaire_app.app.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.solitaire_app.app.entity.VO.HomeResultVO;
import org.jeecg.modules.solitaire_app.app.entity.VO.PriceAvgVO;
import org.jeecg.modules.solitaire_app.app.entity.VO.RefineryPriceChartVO;
import org.jeecg.modules.solitaire_app.app.entity.VO.RefinerytzVO;
import org.jeecg.modules.solitaire_app.app.entity.YsyRefineryChg;
import org.jeecg.modules.solitaire_app.app.entity.YsyRefineryPrice;
import org.jeecg.modules.solitaire_app.app.query.RefineryChartQuery;
import org.jeecg.modules.solitaire_app.app.query.RefineryPriceQuery;
import org.jeecg.modules.solitaire_app.app.query.RefinerytzQuery;
import org.jeecg.modules.solitaire_app.app.service.IYsyRefineryPriceService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 炼厂油品报价
 * @Author: jeecg-boot
 * @Date: 2024-03-25
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "炼厂油品报价")
@RestController
@RequestMapping("/app/ysyRefineryPrice")
public class YsyRefineryPriceController extends JeecgController<YsyRefineryPrice, IYsyRefineryPriceService> {
    @Autowired
    private IYsyRefineryPriceService ysyRefineryPriceService;

    /**
     * 分页列表查询
     *
     * @param ysyRefineryPrice
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "炼厂油品报价-分页列表查询")
    @ApiOperation(value = "炼厂油品报价-分页列表查询", notes = "炼厂油品报价-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(YsyRefineryPrice ysyRefineryPrice,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<YsyRefineryPrice> queryWrapper = QueryGenerator.initQueryWrapper(ysyRefineryPrice, req.getParameterMap());
        Page<YsyRefineryPrice> page = new Page<YsyRefineryPrice>(pageNo, pageSize);
        IPage<YsyRefineryPrice> pageList = ysyRefineryPriceService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 历史报价分页列表查询
     *
     * @param ysyRefineryPrice
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "炼厂油品报价-历史报价分页列表查询")
    @ApiOperation(value = "炼厂油品报价-历史报价分页列表查询", notes = "炼厂油品报价-历史报价分页列表查询")
    @GetMapping(value = "/queryPriceListHisPageList")
    public Result<?> queryPriceListHisPageList(YsyRefineryPrice ysyRefineryPrice,
                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                               HttpServletRequest req) {
        QueryWrapper<YsyRefineryPrice> queryWrapper = QueryGenerator.initQueryWrapper(ysyRefineryPrice, req.getParameterMap());
        Page<YsyRefineryPrice> page = new Page<YsyRefineryPrice>(pageNo, pageSize);
        queryWrapper.orderByDesc("create_time");
        IPage<YsyRefineryPrice> pageList = ysyRefineryPriceService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 首页查询成品油报价
     *
     * @param refineryPriceQuery
     * @return
     */
    @AutoLog(value = "炼厂油品报价-首页查询成品油报价")
    @ApiOperation(value = "炼厂油品报价-首页查询成品油报价", notes = "炼厂油品报价-首页查询成品油报价")
    @PostMapping(value = "/getHomeResult")
    public Result<PageInfo<HomeResultVO>> getHomeResult(@RequestBody RefineryPriceQuery refineryPriceQuery) {
        return ysyRefineryPriceService.getHomeResult(refineryPriceQuery);
    }

    /**
     * 油价历史数据
     *
     * @param map
     * @return
     */
    @AutoLog(value = "炼厂油品报价-油价历史数据")
    @ApiOperation(value = "炼厂油品报价-油价历史数据", notes = "炼厂油品报价-油价历史数据")
    @PostMapping(value = "/getOilTypePriceHis")
    public Result<PageInfo<YsyRefineryPrice>> getOilTypePriceHis(@RequestBody Map<String, Object> map) {
        return ysyRefineryPriceService.getOilTypePriceHis(map);
    }

    /**
     * 报价
     *
     * @param map
     * @return
     */
    @AutoLog(value = "炼厂油品报价-报价")
    @ApiOperation(value = "炼厂油品报价-报价", notes = "炼厂油品报价-报价")
    @PostMapping(value = "/bjPrice")
    public Result<?> bjPrice(@RequestBody Map<String, Object> map) {
        String ysyRefineryId = map.get("ysyRefineryId").toString();
        JSONObject selectObj = new JSONObject(map.get("selectNameList"));
        String oilTypeId = selectObj.getStr("id");

        JSONArray jsonArray = selectObj.getJSONArray("arr");

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            int price = ObjectUtil.isNotEmpty(jsonObject.get("price")) ? jsonObject.getInt("price") : 0;
            int type = jsonObject.getInt("type");//类型，1优惠价，2挂牌价
            if (price > 0) {
                //先查询该炼厂当天的第一条报价
                YsyRefineryPrice ysyRefineryPriceFirst = ysyRefineryPriceService.getOne(Wrappers.<YsyRefineryPrice>lambdaQuery()
                        .eq(YsyRefineryPrice::getYsyRefineryId, ysyRefineryId)
                        .eq(YsyRefineryPrice::getYsyOilTypeId, oilTypeId)
                        .eq(YsyRefineryPrice::getType, type)
                        .apply(true, "DATE_FORMAT(now(),'%Y-%m-%d') = DATE_FORMAT(create_time,'%Y-%m-%d')")
                        .orderByAsc(YsyRefineryPrice::getCreateTime)
                        .last("limit 1")
                );

                int chg = 0;
                if (ObjectUtil.isEmpty(ysyRefineryPriceFirst)) {
                    //如果没报价
                    chg = 0;
                } else {
                    chg = price - ysyRefineryPriceFirst.getPrice();
                }

                YsyRefineryPrice ysyRefineryPrice = new YsyRefineryPrice();
                ysyRefineryPrice.setYsyRefineryId(ysyRefineryId);
                ysyRefineryPrice.setPrice(price);
                ysyRefineryPrice.setCreateTime(new Date());
                ysyRefineryPrice.setChg(chg);
                ysyRefineryPrice.setYsyOilTypeId(oilTypeId);
                ysyRefineryPrice.setType(type);
                ysyRefineryPriceService.save(ysyRefineryPrice);
            }
        }

        return Result.ok("报价成功");
    }

    /**
     * 首页查询成品油报价
     *
     * @param refinerytzQuery
     * @return
     */
    @AutoLog(value = "炼厂油品报价-查询炼厂调整价格")
    @ApiOperation(value = "炼厂油品报价-查询炼厂调整价格", notes = "炼厂油品报价-查询炼厂调整价格")
    @PostMapping(value = "/getRefineryZtData")
    public Result<List<RefinerytzVO>> getRefineryZtData(@RequestBody RefinerytzQuery refinerytzQuery) {
        return ysyRefineryPriceService.getRefineryZtData(refinerytzQuery);
    }

    /**
     * 报价平均值
     *
     * @param refinerytzQuery
     * @return
     */
    @AutoLog(value = "炼厂油品报价-报价平均值")
    @ApiOperation(value = "炼厂油品报价-报价平均值", notes = "炼厂油品报价-报价平均值")
    @GetMapping(value = "/getPriceAvg")
    public Result<List<PriceAvgVO>> getPriceAvg() {
        return ysyRefineryPriceService.getPriceAvg();
    }

    /**
     * 首页查询成品油报价
     *
     * @param refineryChartQuery
     * @return
     */
    @AutoLog(value = "炼厂油品报价-查询炼厂价格折线图")
    @ApiOperation(value = "炼厂油品报价-查询炼厂价格折线图", notes = "炼厂油品报价-查询炼厂价格折线图")
    @PostMapping(value = "/getRefineryPriceChart")
    public Result<List<RefineryPriceChartVO>> getRefineryPriceChart(@RequestBody RefineryChartQuery refineryChartQuery) {
        return ysyRefineryPriceService.getRefineryPriceChart(refineryChartQuery);
    }

    /**
     * 添加
     *
     * @param ysyRefineryPrice
     * @return
     */
    @AutoLog(value = "炼厂油品报价-添加")
    @ApiOperation(value = "炼厂油品报价-添加", notes = "炼厂油品报价-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody YsyRefineryPrice ysyRefineryPrice) {
        ysyRefineryPriceService.save(ysyRefineryPrice);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param ysyRefineryPrice
     * @return
     */
    @AutoLog(value = "炼厂油品报价-编辑")
    @ApiOperation(value = "炼厂油品报价-编辑", notes = "炼厂油品报价-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody YsyRefineryPrice ysyRefineryPrice) {

        YsyRefineryPrice currentPrice = ysyRefineryPriceService.getById(ysyRefineryPrice.getId());
        int topPrice = currentPrice.getPrice() - currentPrice.getChg();
        int chg = ysyRefineryPrice.getPrice() - topPrice;
        ysyRefineryPrice.setChg(chg);
        ysyRefineryPriceService.updateById(ysyRefineryPrice);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "炼厂油品报价-通过id删除")
    @ApiOperation(value = "炼厂油品报价-通过id删除", notes = "炼厂油品报价-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        ysyRefineryPriceService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "炼厂油品报价-批量删除")
    @ApiOperation(value = "炼厂油品报价-批量删除", notes = "炼厂油品报价-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ysyRefineryPriceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "炼厂油品报价-通过id查询")
    @ApiOperation(value = "炼厂油品报价-通过id查询", notes = "炼厂油品报价-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        YsyRefineryPrice ysyRefineryPrice = ysyRefineryPriceService.getById(id);
        return Result.ok(ysyRefineryPrice);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ysyRefineryPrice
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YsyRefineryPrice ysyRefineryPrice) {
        return super.exportXls(request, ysyRefineryPrice, YsyRefineryPrice.class, "炼厂油品报价");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YsyRefineryPrice.class);
    }

}
